package com.example.hospitalsystemclient.ui.Prescription;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hospitalsystemclient.Dataclass.MedicineInfo;
import com.example.hospitalsystemclient.R;

import java.util.List;

public class MedicineAdapter extends RecyclerView.Adapter<MedicineAdapter.MedicineViewHolder> {
    private List<MedicineInfo> medicineList;

    public MedicineAdapter(List<MedicineInfo> medicineList) {
        this.medicineList = medicineList;
    }

    public void update(List<MedicineInfo> newMedicineList) {
        this.medicineList = newMedicineList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MedicineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_medicine, parent, false);
        return new MedicineViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MedicineViewHolder holder, int position) {
        MedicineInfo medicine = medicineList.get(position);
        holder.tvMedicineName.setText(medicine.getMedicineName());
        holder.tvMedicineId.setText("编号："+String.valueOf(medicine.getMedicineId()));
        holder.tvFrequency.setText("频率："+medicine.getFrequency());
        holder.tvDosagePer.setText("剂量："+medicine.getDosagePer());
        holder.tvSpecification.setText("规格："+medicine.getSpecification());
        holder.tvPricePer.setText("单价："+String.format("%.2f", medicine.getPricePer()));
        holder.tvAmount.setText("数量："+String.valueOf(medicine.getAmount()));
    }

    @Override
    public int getItemCount() {
        return medicineList.size();
    }

    public static class MedicineViewHolder extends RecyclerView.ViewHolder {
        public TextView tvMedicineName;
        public TextView tvMedicineId;
        public TextView tvFrequency;
        public TextView tvDosagePer;
        public TextView tvSpecification;
        public TextView tvPricePer;
        public TextView tvAmount;

        public MedicineViewHolder(View itemView) {
            super(itemView);
            tvMedicineName = itemView.findViewById(R.id.tvMedicineName);
            tvMedicineId = itemView.findViewById(R.id.tvMedicineId);
            tvFrequency = itemView.findViewById(R.id.tvFrequency);
            tvDosagePer = itemView.findViewById(R.id.tvDosagePer);
            tvSpecification = itemView.findViewById(R.id.tvSpecification);
            tvPricePer = itemView.findViewById(R.id.tvPricePer);
            tvAmount = itemView.findViewById(R.id.tvAmount);
        }
    }
}